package com.hyt.it.ogt.ks.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

/**
 * @author wuyingge
 * @version 1.0.0
 * @ClassName AnswerOptionUtils.java
 * @Description 传入选项数量，获取单选，多选的选项值  A  B  C  D等
 * @createTime 2022年06月27日 10:12:00
 */
public class AnswerOptionUtils {

    private static final String[] options = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};


    public static List<String>  getByOptionNum(Integer num){
        if(Objects.isNull(num) || 0 == num){
            return new ArrayList<>();
        }
        String[] actOptions = Arrays.copyOfRange(options,0,num);
        return Arrays.asList(actOptions);
    }

    public static void main(String[] args) {
        System.out.println(getByOptionNum(4));
        String a = "A";
        String b = "B";
        String c = "C";
        String d = "C";
        System.out.println(StringUtils.join(a,b));
        String[] strarr = {"A","B","C","D","D"};
        System.out.println(deleteSame(StringUtils.join(strarr,",")));
    }

    /**
     *  假如有有一个字符串，其中可能存在重复的字母（无论大小写）
     *  要进行去重复（无论大小写，大小写视为同一个字母）.
     *  第一步：
     *  先把字符串str转大写和转为字符数组，然后使用一
     *  个LinkedHashMap来记录首次出现的字母和所在字符数组位
     *  置的下标
     *  第二步：
     *  根据LinkedHashMap存放的有序的key对应的value
     *  （即改字母首次出现的位置）对应str转成字符数组后的数组选
     *  择出全部字母
     * @param str
     * @return
     */
    public static String deleteSame(String str){
        if(StringUtils.isBlank(str)){
            return str;
        }
        //先去掉里边的","
        str = str.replaceAll(",","");
        String s = str.toUpperCase();//转大写
        char[] chars = s.toCharArray();//字符串转数组
        //用与记录下标
        LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
        //使用LinkedHashMap是为了key-value在put到map时是有序的，后面使用的时候就是有序的
        for (int i = 0;i<chars.length;++i) {
            if (!map.keySet().contains(chars[i])){
                map.put(chars[i],i);
            }
        }
        List<String> arr = new ArrayList<>(map.size());
        char[] chars1 = str.toCharArray();
        for (Character character : map.keySet()) {//通过map保存的信息将chars1里面逐个首次出现字符加到StringBuffer里
            arr.add(String.valueOf(chars1[map.get(character)]));
        }
        return StringUtils.join(arr,",");
    }

}
